<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children' }"
    >
      <el-table-column prop="id" label="用户编号" sortable width="180">
      </el-table-column>
      <el-table-column prop="username" label="用户名" sortable width="180">
      </el-table-column>
      <el-table-column prop="rolename" label="所属角色"> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="scope">
          <el-button v-if="scope.row.status === 1" type="success"
            >启用</el-button
          >
          <el-button v-else type="danger"> 禁用 </el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="edit(scope.row.uid)"
            >编辑</el-button
          >
          <el-button type="danger" @click="del(scope.row.uid)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="cPage"
      :page-size="pages.size"
    >
    </el-pagination>
  </div>
</template>

<script>
import { adminDel, adminList, adminTotal } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  data() {
    return {
      tableData: [], //菜单列表
      pages: {
        size: 2,
        page: 1,
      },
      total: 0,
    };
  },
  methods: {
    requestAdminList() {
      adminList(this.pages).then((res) => {
        if (
          res.list === null ||
          (res.list.length === 0 && this.pages.page > 1)
        ) {
          this.pages.page = this.pages.page - 1;
          this.requestAdminList();
          return;
        }
        if (res.code === 200) {
          this.tableData = res.list;
        }
      });
    },

    requestCount() {
      adminTotal().then((res) => {
        if (res.code === 200) {
          this.total = res.list[0].total;
        }
      });
    },
    del(uid) {
      // element-ui框架 的messageBox组件
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          adminDel({ uid }).then((res) => {
            if (res.code === 200) {
              successAlert(res.msg);
              this.requestCount();
              this.requestAdminList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    edit(uid) {
      this.$emit("edit", uid);
    },
    cPage(page) {
      this.pages.page = page;
      this.requestAdminList();
    },
  },
  mounted() {
    this.requestCount();
    this.requestAdminList();
    this.event.$on("adminList", (e) => {
      this.requestCount();
      this.requestAdminList();
    });
  },
};
</script>

<style>
</style> 